<?php
	/**
	 * Magento
	 *
	 * NOTICE OF LICENSE
	 *
	 * This source file is subject to the Academic Free License (AFL 3.0)
	 * that is bundled with this package in the file LICENSE_AFL.txt.
	 * It is also available through the world-wide-web at this URL:
	 * http://opensource.org/licenses/afl-3.0.php
	 * If you did not receive a copy of the license and are unable to
	 * obtain it through the world-wide-web, please send an email
	 * to license@magento.com so we can send you a copy immediately.
	 *
	 * DISCLAIMER
	 *
	 * Do not edit or add to this file if you wish to upgrade Magento to newer
	 * versions in the future. If you wish to customize Magento for your
	 * needs please refer to http://www.magento.com for more information.
	 *
	 * @category    design
	 * @package     base_default
	 * @copyright   Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
	 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
	 */
?>
<?php 
    $rest         = '8';
    $setting      = Mage::helper('themeframework/settings'); 
    $width        = "100%";
    $product_id[] = Mage::registry('current_product')->getId();
if (Mage::registry("product")){
    /*先获取是否为产品页面 获取产品最底层的分类id*/
    $categoriesobjs = Mage::registry('product')->getCategoryIds();
    $categoriesobj  = end($categoriesobjs);
    $categories     = prev($categoriesobjs); 
}
    
?>
<div id="em-wrapper-related_products" class="block box-collateral block-related em-line-01">
	<div class="em-block-title">
		<h2><span><?php echo $this->__('Related Products') ?></span></h2>
    </div>


    <div id="em-related_products" class="block-content">
        <div class="products-grid mini-products-list em-related-slider owl-carousel " id="block-related_products">
<!-- you  產品詳細頁關聯產品 -->   
    <?php
        $resultobj  =  array();
        $category   =  Mage::getModel('catalog/category');
        $category   -> load($categories);
        $collection =  $category->getProductCollection();
        foreach ($collection as $product) {
            $resultobj[] = $product->getId();
        }
        $result = array_diff($resultobj,$product_id);/*过滤当前产品*/
        
        if(sizeof($result)>$rest){ /*大于获取值时，随机取出指定数量的产品*/
            $results= array_rand($result,$rest);
            foreach ($results as $val) {
                $val_s[] = $result["$val"];
            }
            $result = $val_s;
        }
        foreach($result as $cc)
            {
                $thisproduct= Mage::getModel('catalog/product')->load($cc);
            
     ?>      
        <div class="item">
            <div class="product-item">
                <a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $this->htmlEscape($thisproduct->getName()) ?>" class="product-image">
                    <img class="em-img-lazy img-responsive" src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'thumbnail')->resize(300) ?>"  width="<?php echo $width;?>" alt="<?php echo $this->htmlEscape($thisproduct->getName()) ?>" />
                </a>
                <div class="product-details product-shop">
                    <p class="product-name">
                        <a href="<?php echo $thisproduct->getProductUrl() ?>"><?php echo $this->htmlEscape($thisproduct->getName()) ?></a>
                    </p>
                    <?php echo $this->getPriceHtml($thisproduct, true, '-related') ?>
                </div>
                
            </div>
        </div>
    <?php
        }
    ?>  
</div>
</div>
</div>
<!-- you  產品詳細頁關聯產品 -->  
 <script type="text/javascript">
//<![CDATA[
    (function($) {
var owl = $('#block-related_products');
if(owl.length){
    owl.owlCarousel({
    // Navigation
    navigation : true,
    navigationText : ["<?php echo $this->__('Previous')?>", "<?php echo $this->__('Next')?>"],
    pagination : false,
    paginationNumbers: false,
    // Responsive
    responsive: true,
    items : 6, /*items above 1200px browser width*/
    itemsDesktop : [1200,6], /*//items between 1199px and 981px*/
    itemsDesktopSmall : [992,5],
    itemsTablet: [768,4],
    itemsMobile : [480,3],
    // CSS Styles
    baseClass : "owl-carousel",
    theme : "owl-theme", transitionStyle : false,
    addClassActive : true,
    });
}




    })(jQuery);
//]]>
</script>
<script type="text/javascript">
	//<![CDATA[
        $$('.related-checkbox').each(function(elem){
        	Event.observe(elem, 'click', addRelatedToProduct)
        });
        
        var relatedProductsCheckFlag = false;
        function selectAllRelated(txt){
        	if (relatedProductsCheckFlag == false) {
        		$$('.related-checkbox').each(function(elem){
        			elem.checked = true;
        		});
        		relatedProductsCheckFlag = true;
        		txt.innerHTML="<?php echo $this->__('unselect all') ?>";
        	} else {
        		$$('.related-checkbox').each(function(elem){
        			elem.checked = false;
        		});
        		relatedProductsCheckFlag = false;
        		txt.innerHTML="<?php echo $this->__('select all') ?>";
        	}
        	addRelatedToProduct();
        };
        
        function addRelatedToProduct(){
        	var checkboxes = $$('.related-checkbox');
        	var values = [];
        	for(var i=0;i<checkboxes.length;i++){
        		if(checkboxes[i].checked) values.push(checkboxes[i].value);
        	}
        	if($('related-products-field')){
        		$('related-products-field').value = values.join(',');
        	}
        }; 
	//]]>
</script>